<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch">
      <el-form-item label="补偿项" prop="itemName">
        <el-input
          v-model="queryParams.itemName"
          placeholder="请输入补偿项"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['compensation:item:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['compensation:item:remove']"
        >删除
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

   <!-- <div class="selectedDv" v-if="ids.length > 0">已选<span style="color:#1890FF;padding:  0 6px;">{{ ids.length }}</span>项
    </div>-->

    <el-table v-loading="loading" :data="EvaluationList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="补偿编码" align="center" prop="itemKey"/>
      <el-table-column label="补偿项" align="center" prop="itemName"/>
      <el-table-column label="补偿金额（元/亩）" align="center" prop="compensation">
        <template slot-scope="scope">
          {{
            scope.row.compensation == 0 ? "-" : (String(scope.row.compensation).replace(/(\d)(?=(?:\d{3})+$)/g, '$1,').split('.').length<2?String(scope.row.compensation).replace(/(\d)(?=(?:\d{3})+$)/g, '$1,')+".00":String(scope.row.compensation).replace(/(\d)(?=(?:\d{3})+$)/g, '$1,'))
          }}
        </template>
      </el-table-column>
      <el-table-column label="类型" align="center" prop="compensationType" :formatter="compensationtypeFormat"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['compensation:item:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['compensation:item:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 添加或修改对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="补偿类型">
          <el-select v-model="form.compensationType" placeholder="请选择" style="width: 100%" @change="selectype">
            <el-option
              v-for=" dict in compensationtype"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="补偿编码" prop="itemKey">
          <el-input v-model="form.itemKey" placeholder="请输入补偿编码"/>
        </el-form-item>
        <el-form-item label="补偿项" prop="itemName">
          <el-input v-model="form.itemName" placeholder="请输入补偿项"/>
        </el-form-item>
        <el-form-item label="补偿金额" prop="compensation">
          <el-input v-model="form.compensation" placeholder="请输入补偿金额"/>
        </el-form-item>

        <el-form-item label="备注" prop="remarks">
          <el-input v-model="form.remarks" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  addEvaluation,
  delEvaluation,
  exportEvaluation,
  getEvaluation,
  listEvaluation,
  updateEvaluation
} from "@/api/pggl/evaluation";

export default {
  name: "Evaluation",
  data() {
    return {
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      EvaluationList: [
        {
          itemKey: 101,
          compensationType: 1,
          itemName: '稻田未种',
          compensation: 100
        },
      ],
      compensationtype: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        id: null,
        pid: null,
        itemName: null,
        money: null,
        remarks: null,
        by1: null,
        by2: null
      },
      // 表单参数
      form: {
        compensationType: '1',
        id: null,
        pid: null,
        name: null,
        money: null,
        remarks: null,
        itemKey: null,
        itemName: null,
        compensation: null,
        by1: null,
        by2: null
      },
      // 表单校验
      rules: {
        itemKey: [
          {required: true, message: "补偿编码不能为空", trigger: "blur"}
        ],
        compensationType: [
          {required: true, message: "补偿类型不能为空", trigger: "blur"}
        ],
        itemName: [
          {required: true, message: "补偿项不能为空", trigger: "blur"}
        ],
        compensation: [
          {required: true, message: "补偿金额不能为空", trigger: "blur"}
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("sys_evaluation_type").then(response => {
      this.compensationtype = response.data;
    });
  },
  methods: {
    selectype(val) {
      this.form.itemKey = parseInt(val)+1;
    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      listEvaluation(this.queryParams).then(response => {
        this.EvaluationList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 支付方式字典翻译
    compensationtypeFormat(row, column) {
      return this.selectDictLabel(this.compensationtype, row.compensationType);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        pid: null,
        name: null,
        money: null,
        remarks: null,
        itemKey: null,
        itemName: null,
        compensation: null,
        compensationType: null,
        by1: null,
        by2: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.itemId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.open = true;
      this.title = "新增";
      this.reset();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.itemId || this.ids;
      getEvaluation(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.itemId != null) {
            updateEvaluation(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addEvaluation(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.itemId || this.ids;
      this.$confirm('是否确认删除数据项?', "警告", {
        cancelButtonClass: 'btn-custom-cancel',
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return delEvaluation(ids);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return exportEvaluation(queryParams);
      }).then(response => {
        this.download(response.msg);
      })
    }
  }
};
</script>
<style>
.btn-custom-cancel {
  float: right;
  margin-left: 10px;
}
.selectedDv {
  background-color: rgba(230, 247, 255, 1);
  box-sizing: border-box;
  border-width: 1px;
  border-style: solid;
  border-color: rgba(186, 231, 255, 1);
  border-radius: 4px;
  padding: 10px;
  margin-bottom: 10px;
}
</style>
